//
//  GroupSetViewController.m
//  AAIM
//
//  Created by awh on 2019/12/1.
//  Copyright © 2019年 AAIM. All rights reserved.
//

#import "GroupSetViewController.h"
#import "EditorGroupInfoViewController.h"
#import "GroupQRCodeViewController.h"
#import "GroupPSWViewController.h"

#import "ContactRequestLogic.h"
#import "GroupInfoModel.h"
#import "ContactModel.h"

#import "GroupMemberView.h"
#import "StartGroupChatVC.h"
#import "ReportViewController.h"
#import "ConversationsViewController.h"
#import "ContactsViewController.h"

@interface GroupSetViewController ()<GroupMemberViewDelegate>
@property(nonatomic,strong)UIScrollView*bgScrollView;
@property(nonatomic,strong)UIView*groupHeadBackView;
@property(nonatomic,strong)UIView*groupMembersBackView;
@property(nonatomic,strong)GroupInfoModel * groupInfomodel;
@property(nonatomic,strong)CustomAlertView * alertView;
@property(nonatomic,strong)GroupMemberView * memberView;
@property(nonatomic,strong)NSMutableArray  * markButtonArray;
@property(nonatomic,strong)NSMutableArray  * groupMemberArray;
@property(nonatomic,strong)UIImage * groupImge;

@end

@implementation GroupSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle=@"群设置";
//    [self addsubViews];
    _markButtonArray=[NSMutableArray new];
    _groupMemberArray=[NSMutableArray new];

    [self loadData];
}
-(void)loadData
{
    KWeakSelf;
    [self showLoadingHud];
    [ContactRequestLogic requestShowGroupSetWithGroupid:_groupId Success:^(NSDictionary * _Nonnull Dict) {
        [self hideHud];
        NSLog(@"群设置=%@",Dict);
        NSDictionary * dataDic=Dict[@"data"];
        weakSelf.groupInfomodel =[GroupInfoModel mj_objectWithKeyValues:dataDic];
        NSArray * joinGroupToList=dataDic[@"joinGroupToList"];
        if (joinGroupToList.count>0) {
            for (NSDictionary * dic in joinGroupToList) {
                ContactModel * model=[ContactModel mj_objectWithKeyValues:dic];
                [self.groupMemberArray addObject:model];
            }
        }
      __block  BOOL hiddenDeleBtn;
        [self.groupMemberArray enumerateObjectsUsingBlock:^(ContactModel* obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.remark isEqualToString:[LoginManager shareLoginManager].userModel.nickname] ) {
                if ([obj.isMaster isEqualToString:@"1"]) {
                   hiddenDeleBtn = NO;
                }else{
                   hiddenDeleBtn = YES;
                }
                *stop = true;
            }
        }];
        if (self.groupMemberArray.count>10) {
            if (hiddenDeleBtn) {
                NSIndexSet *indexset = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,9)];//范围0，1，2个元素
                [self.groupMemberArray objectsAtIndexes:indexset];
                
                    ContactModel * model=[ContactModel new];
                    
                        model.head=@"加 (5) copy";
                    
                    [self.groupMemberArray addObject:model];
                
            }else{
            NSIndexSet *indexset = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,8)];//范围0，1，2个元素
            [self.groupMemberArray objectsAtIndexes:indexset];
            for (int i=0; i<2; i++) {
                ContactModel * model=[ContactModel new];
                if (i==0) {
                    model.head=@"加 (5) copy";
                }else
                {
                   
                       model.head=@"减";
                    
                }
                [self.groupMemberArray addObject:model];
            }
            }
        }else if (self.groupMemberArray.count==10)
        {
            if (hiddenDeleBtn) {
                ContactModel * model=[ContactModel new];
                        model.head=@"加 (5) copy";
                [self.groupMemberArray replaceObjectAtIndex:9 withObject:model];
            }else{
                
            ContactModel * model=[ContactModel new];
                    model.head=@"加 (5) copy";
            [self.groupMemberArray replaceObjectAtIndex:8 withObject:model];
            
                ContactModel * model1=[ContactModel new];
                 model1.head=@"减";
                 [self.groupMemberArray replaceObjectAtIndex:9 withObject:model1];
                
            }
        }else if (self.groupMemberArray.count==9)
        {
            if (hiddenDeleBtn) {
                ContactModel * model=[ContactModel new];
                           model.head=@"加 (5) copy";
                    [self.groupMemberArray addObject:model];
            }else{
            ContactModel * model=[ContactModel new];
            model.head=@"加 (5) copy";
            [self.groupMemberArray replaceObjectAtIndex:8 withObject:model];
                  ContactModel * model1=[ContactModel new];
                  model1.head=@"减";
                  [self.groupMemberArray addObject:model1];
               
            }
        }else
        {
            int count;
             if (!hiddenDeleBtn) {
                 count = 2;
             }else{
                 count = 1;
             }
            for (int i=0; i<count; i++) {
                ContactModel * model=[ContactModel new];
                if (i==0) {
                    model.head=@"加 (5) copy";
                }else
                {
                     model.head=@"减";
                    
                }
                [self.groupMemberArray addObject:model];
            }
        }
        [self addsubViews];
    } failure:^(NSString * _Nonnull errMsg) {
        [self hideHud];
        [self showTipHudWithText:errMsg];
    }];
    
//    [ContactRequestLogic requestGroupqueryMembersWithgroupId:_groupId Query:@"" page:@"1" Success:^(NSDictionary * _Nonnull Dict) {
//        AAIMLog(@"群成员=%@",Dict);
//        KWeakSelf;
//        if ([Dict[@"code"] integerValue] == 200) {
//            for (NSDictionary * dic in Dict[@"data"][@"list"]) {
//                ContactModel * memberModel = [ContactModel mj_objectWithKeyValues:Dict[@"data"]];
//                [weakSelf.groupMemberArray addObject:memberModel];
//            }
//           
//            //刷新
//        }
//        
//        [weakSelf hideHud];
//    } failure:^(NSString * _Nonnull errMsg) {
//        [self showTipHudWithText:errMsg];
//    }];
    
}
-(void)addsubViews
{
    [self.view addSubview:self.bgScrollView];
    [self.bgScrollView addSubview:self.groupHeadBackView];
    if (self.groupMemberArray.count>5) {
         self.memberView=[[GroupMemberView alloc]initWithFrame:CGRectMake(0, _groupHeadBackView.bottom+10, SCREEN_WIDTH, 205) dataArray:self.groupMemberArray withIsMaster:_groupInfomodel.isMaster];
    }else
    {
        self.memberView=[[GroupMemberView alloc]initWithFrame:CGRectMake(0, _groupHeadBackView.bottom+10, SCREEN_WIDTH, 130) dataArray:self.groupMemberArray withIsMaster:_groupInfomodel.isMaster];
         self.memberView.backgroundColor=[UIColor whiteColor];
    }
    self.memberView.backgroundColor = [UIColor whiteColor];
    self.memberView.delegate=self;
    [self.bgScrollView addSubview:self.memberView];
    NSArray * btnName;
    if ([_groupInfomodel.isMaster isEqualToString:@"1"]) {
     btnName=  [self isMasterUI];
    }else
    {
     btnName=  [self isMembersUI];
    }
    PublicButton * markBtn=[self.view viewWithTag:btnName.count];
    [_bgScrollView setContentSize:CGSizeMake(0, markBtn.bottom+10)];
    UIImageView * headImage=[self.view viewWithTag:100];

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",userHeaderImage,_groupInfomodel.groupHead]]];
    headImage.image = [UIImage imageWithData:data];
    _groupImge = headImage.image;

}
-(NSArray*)isMasterUI
{
    NSArray*btnName=@[@"群聊名称",@"群口令",@"群二维码",@"群公告",@"是否禁言",@"置顶会话",@"消息免打扰",@"允许搜索该群",@"是否允许任何人进群",@"清空聊天记录",@"举报",@"退出群组"];
    for (int i=0; i<btnName.count; i++) {
        PublicButton * cellButton=[[PublicButton alloc]initWithFrame:CGRectMake(0, self.memberView.bottom+10+(50+0)*i, SCREEN_WIDTH, 50) withButtonType:@"groupSetbutton"];
        cellButton.titleLbl.text=btnName[i];
        cellButton.tag=i+1;
        cellButton.backgroundColor=[UIColor whiteColor];
        if (i==0||i==1||i==2||i==3) {
            cellButton.swicthButton.hidden=YES;
            if (i==0) {
                cellButton.remarkLbl.hidden=NO;
                cellButton.remarkLbl.textAlignment=2;
                cellButton.remarkLbl.text=_groupInfomodel.groupName;
            }
        }else
        {
            cellButton.rigthImage.hidden=YES;
        }
        if (i==2) {
            cellButton.lineV.hidden=YES;
        }
        if (i==3) {
            cellButton.y=self.memberView.bottom+10+(50+0)*i+10;
            cellButton.height=75;
            cellButton.remarkLbl.hidden=NO;
            cellButton.remarkLbl.frame=CGRectMake(15, cellButton.titleLbl.bottom+5, SCREEN_WIDTH-30, 15);
            if ([NSObject ldy_isEmpty:_groupInfomodel.groupNotice]) {
                cellButton.remarkLbl.text=@"签名：这个群主很懒，什么都没留下…";
            }else
            {
                cellButton.remarkLbl.text=_groupInfomodel.groupNotice;
            }
            cellButton.lineV.hidden=YES;
        }
       /* if (i==4||i==5||i==6) {
            CGFloat groupheight = self.memberView.bottom+10+10+35;
            cellButton.y=groupheight+(50+0)*i;
            if (i==4) {
                cellButton.swicthButton.selected=[_groupInfomodel.isGroupmember boolValue];
            }else if (i==5)
            {
               cellButton.swicthButton.selected= [_groupInfomodel.isGroupadd boolValue];
            }else if (i==6) {
              cellButton.swicthButton.selected= [_groupInfomodel.isPrivatechat boolValue];
              cellButton.lineV.hidden=YES;
            }
            } */
        if (i==4) {
            cellButton.swicthButton.selected= ![_groupInfomodel.isChat boolValue];
            CGFloat groupheight = self.memberView.bottom+10+35+10;
            cellButton.y=groupheight+(50+0)*i;
            cellButton.lineV.hidden=YES;
            
        }
        if (i==5||i==6||i==7||i==8) {
            CGFloat groupheight = self.memberView.bottom+10+35+10+10;
            cellButton.y=groupheight+(50+0)*i;
            if (i==5) {
                cellButton.swicthButton.selected= [_groupInfomodel.isStick boolValue];
            }else if (i==6)
            {
                cellButton.swicthButton.selected= [_groupInfomodel.isDis boolValue];
            }else if (i==7)
            {
                cellButton.swicthButton.selected= [_groupInfomodel.isSelect boolValue];
            }
            if (i==8) {
                cellButton.swicthButton.selected= [_groupInfomodel.isAdd boolValue];
                cellButton.lineV.hidden=YES;
            }
        }
        if (i==9||i==10) {
            CGFloat groupheight = self.memberView.bottom+10+35+10+10+10;
            cellButton.y=groupheight+(50+0)*i;
//            cellButton.lineV.hidden=YES;
            cellButton.swicthButton.hidden=YES;
        }
        if (i==10) {
            CGFloat groupheight = self.memberView.bottom+10+35+10+10+10;
            cellButton.y=groupheight+(50+0)*i;
            cellButton.lineV.hidden=YES;
            cellButton.swicthButton.hidden=YES;
        }
        if (i==11) {
            CGFloat groupheight = self.memberView.bottom+10+35+10+10+10+10;
            cellButton.y=groupheight+(50+0)*i;
            cellButton.titleLbl.x=SCREEN_WIDTH/2-80;
            cellButton.titleLbl.textColor=KHEXCOLOR(@"#D0021B");
            cellButton.titleLbl.textAlignment=1;
            cellButton.lineV.hidden=YES;
            cellButton.swicthButton.hidden=YES;
        }
        [cellButton addTarget:self action:@selector(GroupManagerClickCellButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        cellButton.swicthButton.tag=i+1;
        [cellButton.swicthButton addTarget:self action:@selector(GroupManagerClickCellSwichBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.bgScrollView addSubview:cellButton];
    }
    return btnName;
}
-(NSArray*)isMembersUI
{
   NSArray * btnName=@[@"群聊名称",@"群口令",@"群二维码",@"群公告",@"置顶会话",@"消息免打扰",@"清空聊天记录",@"举报",@"退出群组"];
    for (int i=0; i<btnName.count; i++) {
        PublicButton * cellButton=[[PublicButton alloc]initWithFrame:CGRectMake(0, self.memberView.bottom+10+(50+0)*i, SCREEN_WIDTH, 50) withButtonType:@"groupSetbutton"];
        cellButton.titleLbl.text=btnName[i];
        cellButton.tag=i+1;
        cellButton.backgroundColor=[UIColor whiteColor];
//        if (i==0) {
//            cellButton.remarkLbl.textAlignment=2;
//            cellButton.remarkLbl.text=_groupInfomodel.groupName;
//            cellButton.swicthButton.hidden=cellButton.rigthImage.hidden=YES;
//        }
//        if (i==1) {
//            cellButton.swicthButton.hidden=YES;
//        }
        if (i==0||i==1||i==2) {
            cellButton.swicthButton.hidden=YES;
            if (i==0) {
                cellButton.remarkLbl.hidden=NO;
                cellButton.remarkLbl.textAlignment=2;
                cellButton.rigthImage.hidden=YES;
                cellButton.remarkLbl.text=_groupInfomodel.groupName;
            }
        }
        
        if (i==3) {
            cellButton.swicthButton.hidden=YES;
            cellButton.height=75;
            cellButton.remarkLbl.hidden=NO;
            cellButton.lineV.hidden=YES;
            cellButton.rigthImage.hidden=YES;
            cellButton.remarkLbl.frame=CGRectMake(15, cellButton.titleLbl.bottom+5, SCREEN_WIDTH-30, 15);
            if ([NSObject ldy_isEmpty:_groupInfomodel.groupNotice]) {
                cellButton.remarkLbl.text=@"签名：这个群主很懒，什么都没留下…";
            }else
            {
                cellButton.remarkLbl.text=_groupInfomodel.groupNotice;
            }
        }
        if (i==4||i==5) {
            CGFloat groupheight = self.memberView.bottom+10+35;
            cellButton.y=groupheight+(50+0)*i;
            cellButton.rigthImage.hidden=YES;
            if (i==4) {
                cellButton.swicthButton.selected= [_groupInfomodel.isStick boolValue];
            }else
            {
                cellButton.swicthButton.selected= [_groupInfomodel.isDis boolValue];
            }
            if (i==5) {
                cellButton.lineV.hidden=YES;
            }
        }
        if (i==6||i==7) {
            CGFloat groupheight = self.memberView.bottom+10+35+10;
            cellButton.y=groupheight+(50+0)*i;
            cellButton.swicthButton.hidden=cellButton.rigthImage.hidden=YES;
        }
        if (i==7) {
            CGFloat groupheight = self.memberView.bottom+10+35+10;
            cellButton.y=groupheight+(50+0)*i;
            cellButton.lineV.hidden=YES;
            cellButton.swicthButton.hidden=YES;
        }
        if (i==8) {
            CGFloat groupheight = self.memberView.bottom+35+10+10+10;
            cellButton.y=groupheight+(50+0)*i;
            cellButton.titleLbl.x=SCREEN_WIDTH/2-80;
            cellButton.titleLbl.textColor=KHEXCOLOR(@"#D0021B");
            cellButton.titleLbl.textAlignment=1;
            cellButton.lineV.hidden=YES;
            cellButton.swicthButton.hidden=YES;
            cellButton.rigthImage.hidden=YES;
        }
        [cellButton addTarget:self action:@selector(GroupMemberClickCellButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        cellButton.swicthButton.tag=i+1;
        [cellButton.swicthButton addTarget:self action:@selector(GroupMembersClickCellSwichBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.bgScrollView addSubview:cellButton];
    }
    return btnName;
}
#pragma mark - buttonAction
-(void)GroupManagerClickCellButtonAction:(PublicButton*)sender
{
    KWeakSelf;
    switch (sender.tag) {
        case 1:
        {
            EditorGroupInfoViewController * infoVC=[[EditorGroupInfoViewController alloc]init];
            infoVC.type=@"groupName";
            infoVC.editorBlock = ^(NSString * _Nonnull text) {
                weakSelf.groupInfomodel.groupName=text;
                [self SaveGroupSettinngAction];
            };
            [self.navigationController pushViewController:infoVC animated:YES];
        }
            break;
        case 2:
            NSLog(@"群口令");
        {
            GroupPSWViewController*PSWVC=[[GroupPSWViewController alloc]init];
            PSWVC.groupHead=_groupInfomodel.groupHead;
            PSWVC.groupCode = _groupInfomodel.groupCode;
            PSWVC.editorBlock = ^(NSString * _Nonnull text) {
                weakSelf.groupInfomodel.groupCode=text;
                [self SaveGroupSettinngAction];
            };
            [self.navigationController pushViewController:PSWVC animated:YES];
        }
            break;
        case 3:
            NSLog(@"群二维码");
        {
            GroupQRCodeViewController*qrcodeVC=[[GroupQRCodeViewController alloc]init];
            qrcodeVC.QRUrl=[NSString stringWithFormat:@"%@",_groupInfomodel.groupQr];
            qrcodeVC.groupHeadImage=_groupImge;
            qrcodeVC.groupName=_groupInfomodel.groupName;
            [self.navigationController pushViewController:qrcodeVC animated:YES];
        }
            break;
        case 4:
        {
            EditorGroupInfoViewController * infoVC=[[EditorGroupInfoViewController alloc]init];
            infoVC.type=@"announcement";
            infoVC.editorBlock = ^(NSString * _Nonnull text) {
                weakSelf.groupInfomodel.groupNotice=text;
                [self SaveGroupSettinngAction];
            };
            [self.navigationController pushViewController:infoVC animated:YES];
        }
            break;
        case 10:
        {
            NSLog(@"清空聊天记录");
            _alertView = [[CustomAlertView alloc] initWithFrame:self.view.bounds withType:@"3" title:@"确认清空聊天记录"];
            _alertView.clickBlock = ^(NSString * _Nonnull selectType, NSString * _Nonnull textf, NSString * _Nonnull remarkStr) {
                if ([selectType isEqualToString:@"2"]) {
                    [[GroupMessageDataBase shareGroupMessageDataBase] clearAllMessageWithGroupId:[weakSelf.groupId integerValue]];
                    if (weakSelf.delegate) {
                        [weakSelf.delegate clearGroupChatHistory];
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                               [weakSelf showTipHudWithText:@"清空成功"];
                                           });
                    }
                   
                }
                [weakSelf.alertView removeFromSuperview];
            };
            [[UIApplication sharedApplication].keyWindow addSubview:_alertView];
        }
            
            break;
        case 11:
        {
            NSLog(@"举报");
            ReportViewController * reportVC = [[ReportViewController alloc]init];
            reportVC.reportType = @"group";
            reportVC.fuuid = _groupId;
            [self.navigationController pushViewController:reportVC animated:YES];
        }
            break;
        case 12:
        {
            NSLog(@"退出群组");
            NSString * titleStr;
            titleStr=@"您将退出并解散群组";
            _alertView = [[CustomAlertView alloc] initWithFrame:self.view.bounds withType:@"3" title:titleStr];
            _alertView.clickBlock = ^(NSString * _Nonnull selectType, NSString * _Nonnull textf, NSString * _Nonnull remarkStr) {
                if ([selectType isEqualToString:@"2"]) {
                    [weakSelf exitGroupAction];
                }
                [weakSelf.alertView removeFromSuperview];
            };
            [[UIApplication sharedApplication].keyWindow addSubview:_alertView];
        }
            break;
        default:
            break;
    }
}
-(void)GroupManagerClickCellSwichBtnAction:(UIButton*)sender
{
    sender.selected=!sender.selected;
     NSLog(@"选中==%ld",sender.tag);
    switch (sender.tag) {
        case 5:
        {
            NSLog(@"是否禁言");
            _groupInfomodel.isChat=[NSString stringWithFormat:@"%d",!sender.selected];
        }
            break;
        case 6:
            //置顶聊天
            _groupInfomodel.isStick=[NSString stringWithFormat:@"%d",sender.selected];
            [[ConversationListDataBase shareConversationListDataBase] updateConversationTopStatusWithChatType:ChatTypeGroup uidOrGroupId:[self.groupId integerValue] isTop:sender.isSelected];
            break;
        case 7:
            //消息免打扰
            _groupInfomodel.isDis=[NSString stringWithFormat:@"%d",sender.selected];
            [[ConversationListDataBase shareConversationListDataBase] updateMessageIgnoreStatusWithChatType:ChatTypeGroup uidOrGroupId:[self.groupId integerValue] isIgnore:sender.isSelected];
            break;
        case 8:
            _groupInfomodel.isSelect=[NSString stringWithFormat:@"%d",sender.selected];
            break;
        case 9:
            _groupInfomodel.isAdd=[NSString stringWithFormat:@"%d",sender.selected];
            break;
        case 10:
//            _groupInfomodel.isDis=[NSString stringWithFormat:@"%d",sender.selected];
            break;
        case 11:
//            _groupInfomodel.isSelect=[NSString stringWithFormat:@"%d",sender.selected];
            break;
        case 12:
//            _groupInfomodel.isAdd=[NSString stringWithFormat:@"%d",sender.selected];
            break;
        default:
            break;
    }
    [self SaveGroupSettinngAction];
}
-(void)GroupMemberClickCellButtonAction:(PublicButton*)sender
{     KWeakSelf;
    switch (sender.tag) {
        case 2:
        {
            NSLog(@"群口令");
            GroupPSWViewController*PSWVC=[[GroupPSWViewController alloc]init];
            PSWVC.groupHead=_groupInfomodel.groupHead;
            PSWVC.editorBlock = ^(NSString * _Nonnull text) {
                weakSelf.groupInfomodel.groupCode=text;
                [self SaveGroupSettinngAction];
            };
            PSWVC.groupCode=_groupInfomodel.groupCode;
            [self.navigationController pushViewController:PSWVC animated:YES];
        }
            break;
            case 3:
        {
            NSLog(@"群二维码");
            GroupQRCodeViewController*qrcodeVC=[[GroupQRCodeViewController alloc]init];
            qrcodeVC.groupName = _groupInfomodel.groupName;
            qrcodeVC.QRUrl=_groupInfomodel.groupQr;
            qrcodeVC.groupHeadImage = _groupImge;
            [self.navigationController pushViewController:qrcodeVC animated:YES];
            
        }
            break;
        case 7:
        {
            NSLog(@"清空聊天记录");
            _alertView = [[CustomAlertView alloc] initWithFrame:self.view.bounds withType:@"3" title:@"确认清空聊天记录"];
            _alertView.clickBlock = ^(NSString * _Nonnull selectType, NSString * _Nonnull textf, NSString * _Nonnull remarkStr) {
                if ([selectType isEqualToString:@"2"]) {
                   [[GroupMessageDataBase shareGroupMessageDataBase] clearAllMessageWithGroupId:[weakSelf.groupId integerValue]];
                          if (weakSelf.delegate) {
                                     [weakSelf.delegate clearGroupChatHistory];
                                     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                                            [weakSelf showTipHudWithText:@"清空成功"];
                                                        });
                                 }
                }
                [weakSelf.alertView removeFromSuperview];
            };
            [[UIApplication sharedApplication].keyWindow addSubview:_alertView];
        }
            break;
        case 8:
        {
            NSLog(@"举报");
            ReportViewController * reportVC = [[ReportViewController alloc]init];
            reportVC.reportType = @"group";
            reportVC.fuuid = _groupId;
            [self.navigationController pushViewController:reportVC animated:YES];
        }
            break;
            case 9:
        {
            NSLog(@"退出群组");
            NSString * titleStr;
            if ([_groupInfomodel.isMaster isEqualToString:@"1"]) {
                titleStr=@"您将退出并解散群组";
            }else
            {
                titleStr=@"您将退出群聊";
            }
            _alertView = [[CustomAlertView alloc] initWithFrame:self.view.bounds withType:@"3" title:titleStr];
            _alertView.clickBlock = ^(NSString * _Nonnull selectType, NSString * _Nonnull textf, NSString * _Nonnull remarkStr) {
                if ([selectType isEqualToString:@"2"]) {
                    [weakSelf exitGroupAction];
                }
                [weakSelf.alertView removeFromSuperview];
            };
            [[UIApplication sharedApplication].keyWindow addSubview:_alertView];
        }
            break;
        default:
            break;
    }
}
-(void)GroupMembersClickCellSwichBtnAction:(UIButton*)sender
{
    sender.selected=!sender.selected;
    NSLog(@"选中==%ld",sender.tag);
    switch (sender.tag) {
        case 5:
            _groupInfomodel.isStick=[NSString stringWithFormat:@"%d",sender.selected];
            [[ConversationListDataBase shareConversationListDataBase] updateConversationTopStatusWithChatType:ChatTypeGroup uidOrGroupId:[self.groupId integerValue] isTop:sender.isSelected];
            break;
        case 6:
            _groupInfomodel.isDis=[NSString stringWithFormat:@"%d",sender.selected];
            [[ConversationListDataBase shareConversationListDataBase] updateMessageIgnoreStatusWithChatType:ChatTypeGroup uidOrGroupId:[self.groupId integerValue] isIgnore:sender.isSelected];

            break;
        default:
            break;
    }
    [self SaveGroupSettinngAction];
}
#pragma mark - RequestLogic
-(void)exitGroupAction
{
    [ContactRequestLogic requestQuitGroupWithGroupid:_groupId Success:^(NSDictionary * _Nonnull Dict) {
        //退出群聊 删除会话记录 聊天记录 本地群 发送im
        NSLog(@"退出群聊：%@",Dict);
        [self showTipHudWithText:Dict[@"message"]];
        [[UserDataBaseTool shareUserDataBaseTool] deleteGroup:self.groupId];
        //删除聊天记录
        [[GroupMessageDataBase shareGroupMessageDataBase] clearAllMessageWithGroupId:[self.groupId integerValue]];
//        删除会话记录
        [[ConversationManager shareManager] deleteConversationWithKeyid:[NSString stringWithFormat:@"group_%@",self.groupId]];
        [[ConversationListDataBase shareConversationListDataBase] deleteConversationWithKeyid:[NSString stringWithFormat:@"group_%@",self.groupId]];
        
//       发通知 刷新首页数据
//        [[NSNotificationCenter defaultCenter] postNotificationName:cChatRecordMessage object:nil];
        [[UserDataBaseTool shareUserDataBaseTool] reloadData:2 Success:^(BOOL isOK) {
            if (isOK) {
                [[NSNotificationCenter defaultCenter] postNotificationName:kMessageSystemCreateGroup object:nil];
            }
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
          for (UIViewController *controller in self.navigationController.viewControllers) {

              ConversationsViewController *chatVC =(ConversationsViewController *)controller;

              [self.navigationController popToViewController:chatVC animated:NO];
               }
        });
        
    } failure:^(NSString * _Nonnull errMsg) {
        [self showTipHudWithText:errMsg];
    }];
}
-(void)SaveGroupSettinngAction
{
    KWeakSelf;
    [ContactRequestLogic requestSaveGroupSetWithGroupId:self.groupId GroupCode:_groupInfomodel.groupCode groupName:_groupInfomodel.groupName groupNotice:_groupInfomodel.groupNotice groupQr:_groupInfomodel.groupQr isAdd:_groupInfomodel.isAdd isChat:_groupInfomodel.isChat isDis:_groupInfomodel.isDis isGroupadd:_groupInfomodel.isGroupadd isGroupmember:_groupInfomodel.isGroupmember isPrivatechat:_groupInfomodel.isPrivatechat isSelect:_groupInfomodel.isSelect isStick:_groupInfomodel.isStick Success:^(NSDictionary * _Nonnull Dict) {
        NSLog(@"群设置保存结果==%@",Dict);
        if ([Dict[@"code"] integerValue] == 200) {
            PublicButton * groupNameBtn = [weakSelf.view viewWithTag:1];
            if (![groupNameBtn.remarkLbl.text isEqualToString:weakSelf.groupInfomodel.groupName]) {
                [[UserDataBaseTool shareUserDataBaseTool] reloadData:2 Success:^(BOOL isOK) {
                           if (isOK) {
                               [[NSNotificationCenter defaultCenter] postNotificationName:kMessageSystemCreateGroup object:nil];
                           }
                       }];
                if (self.delegate) {
                    [self.delegate refreshGroupTitle:weakSelf.groupInfomodel.groupName];
                }
            }
            groupNameBtn.remarkLbl.text=self.groupInfomodel.groupName;
            PublicButton * groupAnnouncement = [self.view viewWithTag:4];
            if (self.groupInfomodel.groupNotice &&![CommonTools isEmpty:self.groupInfomodel.groupNotice]) {
                groupAnnouncement.remarkLbl.text=self.groupInfomodel.groupNotice;
            }else{
                groupAnnouncement.remarkLbl.text= @"签名：这个群主很懒，什么都没留下...";
            }
            
            [self showTipHudWithText:@"设置成功"];
        }else{
             [self showTipHudWithText:Dict[@"message"]];
        }
    } failure:^(NSString * _Nonnull errMsg) {
        [self showTipHudWithText:errMsg];
    }];
}

- (void)groupMemberDetail{
    [self didMembersViewWithType:@"displayMembers"];
}

#pragma mark-GroupMemberDelagate
-(void)didMembersViewWithType:(NSString*)type
{
    if ([type isEqualToString:@"delete"]) {
        NSLog(@"删除群成员");
        StartGroupChatVC * vc= [[StartGroupChatVC alloc]init];
        vc.type=3;
        vc.groupID = self.groupId;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([type isEqualToString:@"add"]) {
        NSLog(@"添加群成员");
        StartGroupChatVC * vc= [[StartGroupChatVC alloc]init];
        vc.type=2;
        vc.groupID = self.groupId;
        vc.groupMemberAry = self.groupMemberArray;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        StartGroupChatVC * vc= [[StartGroupChatVC alloc]init];
        vc.type=4;
        vc.groupID = self.groupId;
        vc.isMaster = [_groupInfomodel.isMaster integerValue];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
#pragma mark- get
-(UIScrollView*)bgScrollView
{
    if (!_bgScrollView) {
        _bgScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,NavigationBarHeight, SCREEN_WIDTH,SCREEN_HEIGHT-NavigationBarHeight)];
        _bgScrollView.showsVerticalScrollIndicator=NO;
        _bgScrollView.showsHorizontalScrollIndicator=NO;
        _bgScrollView.backgroundColor=KHEXCOLOR(@"#F4F4F4");
        [_bgScrollView setContentSize:CGSizeMake(0, kScreenHeight)];
    }
    return _bgScrollView;
}
-(UIView*)groupHeadBackView
{
    if (!_groupHeadBackView) {
        _groupHeadBackView=[[UIView alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 65)];
        _groupHeadBackView.backgroundColor=[UIColor whiteColor];
        
        UILabel * title=[[UILabel alloc]initWithFrame:CGRectMake(15, _groupHeadBackView.height/2-9, 180, 18)];
        title.text=@"群头像";
        title.font=[UIFont systemFontOfSize:15];
        title.textColor=KHEXCOLOR(AllBackColor_titleColor);
        [_groupHeadBackView addSubview:title];
        
        UIImageView * image=[[UIImageView alloc]initWithFrame:CGRectMake(_groupHeadBackView.width-15-50, 7.5, 50, 50)];
//        image.backgroundColor=[UIColor redColor];
        image.tag=100;
        image.layer.masksToBounds=YES;
        image.layer.cornerRadius=25;
        [_groupHeadBackView addSubview:image];
    }
    return _groupHeadBackView;
}
//-(GroupMemberView*)memberView
//{
//    if (!_memberView) {
//        _memberView=[[GroupMemberView alloc]initWithFrame:CGRectMake(0, _groupHeadBackView.bottom+10, SCREEN_WIDTH, 205)];
//        _memberView.backgroundColor=[UIColor whiteColor];
//    }
//    return _memberView;
//}
-(UIView*)groupMembersBackView
{
    if (!_groupMembersBackView) {
        _groupMembersBackView=[[UIView alloc]initWithFrame:CGRectMake(0, _groupHeadBackView.bottom+10, SCREEN_WIDTH, 215)];
        _groupMembersBackView.backgroundColor=[UIColor whiteColor];
        
        UILabel * title=[[UILabel alloc]initWithFrame:CGRectMake(15, 15, 180, 18)];
        title.text=@"群成员";
        title.font=[UIFont systemFontOfSize:15];
        title.textColor=KHEXCOLOR(AllBackColor_titleColor);
        [_groupMembersBackView addSubview:title];
        
        UIImageView * image=[[UIImageView alloc]initWithFrame:CGRectMake(_groupMembersBackView.width-30, title.y, 15, 15)];
        image.image=[UIImage imageNamed:@"群设置-返回键"];
        image.contentMode=UIViewContentModeScaleAspectFit;
        [_groupMembersBackView addSubview:image];
    }
    return _groupMembersBackView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
